package com.jingdianjichi.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jingdianjichi.oss.service.FileService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RefreshScope
public class FileController {

    @Resource
    private FileService fileService;

    @NacosValue(value = "${storage.service.type}",autoRefreshed = true)
    private String storageType;

    @RequestMapping("/test")
    public String test() throws Exception {
        List<String> list = fileService.getAllBucket();
        if(list.isEmpty()){
            return "ok";
        }
        return list.get(0);
    }

    @RequestMapping("/test2")
    public String test2() throws Exception {
        return storageType;
    }
    @RequestMapping("/getUrl")
    public String getUrl(String bucketName,String objectName){
        return fileService.getUrl(bucketName,objectName);
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile multipartFile, String bucketName, String objectName) throws Exception {
        return fileService.uploadFile(multipartFile,bucketName,objectName);
    }


}
